define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'payment/order/index' + location.search,
                    add_url: 'payment/order/add',
                    edit_url: 'payment/order/edit',
                    del_url: 'payment/order/del',
                    multi_url: 'payment/order/multi',
                    import_url: 'payment/order/import',
                    table: 'payment_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_number', title: __('Order_number'), operate: 'LIKE'},
                        {field: 'payment_channel_id', title: __('Payment_channel_id'),addclass:'selectpage', data : 'data-source="payment\\channel\\index" data-field="title"', formatter: function(value, row, index) {
                            if (row.channel != undefined) {
                                return row.channel.title;
                            }
                            return '渠道已被删除';
                        }},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'pay_price', title: __('Pay_price'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"wait":__('Status wait'),"pay":__('Status pay'),"cancesl":__('Status cancel'),"refund":__('Status refund')}, formatter: Table.api.formatter.status},
                        {field: 'hook_status', title: __('Hook_status'), searchList: {"wait":__('Hook_status wait'),"success":__('Hook_status success'),"error":__('Hook_status error')}, formatter: Table.api.formatter.status},
                        {field: 'out_order_number', title: __('Out_order_number'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons : [
                            {
                                name: 'pay',
                                text: __('虚拟支付'),
                                title: __('虚拟支付'),
                                classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                url: 'payment/order/pay',
                                confirm: '无需真实支付，确认手动支付吗？',
                                visible: function (data) {
                                    if (data.status != 'wait') {
                                        return false;
                                    }
                                    return true;
                                },
                                success: function (data, ret) {
                                    Layer.alert(ret.msg);
                                },
                                error: function (data, ret) {
                                    Layer.alert(ret.msg);
                                    return false;
                                }
                            },
                            {
                                name: 'dohook',
                                text: __('触发回调'),
                                title: __('触发回调'),
                                classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                url: 'payment/order/dohook',
                                confirm: '确认触发一次回调吗？',
                                visible: function (data) {
                                    if (data.hook_status != 'wait') {
                                        return false;
                                    }
                                    return true;
                                },
                                success: function (data, ret) {
                                    Layer.alert(ret.msg);
                                },
                                error: function (data, ret) {
                                    Layer.alert(ret.msg);
                                    return false;
                                }
                            }
                        ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'payment/order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'payment/order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'payment/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});